package com.ks.repair.service;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.core.base.OperationContext;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.util.ToolUtil;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wanglicheng on 2020/12/9.
 * 可以从配置文件中读取一些配置用于控制Log，MQ，MSG等基础功能
 * 此处如果逻辑相对通用，应考虑把常用逻辑放到croe工程中，以便其它工程的_ServiceBase也可以共用
 * 此处可以用来加载配置文件里的参数，并应用到Croe工程中提供的方法中
 */
public class _ServiceBase<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {


    public String getUserId(String token) {
        if (ToolUtil.isNotEmpty(token)) {
            String[] split = token.split(":");
            String s = split[3];
            if (ToolUtil.isNotEmpty(s)) {
                return s;
            }
        }
        return null;
    }

    public String getTenantId(String token){
        if (ToolUtil.isNotEmpty(token)) {
            String[] split = token.split(":");
            String s = split[0];
            if (ToolUtil.isNotEmpty(s)) {
                return s;
            }
        }
        return null;
    }

    public String getOrgId(String token){
        if (ToolUtil.isNotEmpty(token)) {
            String[] split = token.split(":");
            String s = split[1];
            if (ToolUtil.isNotEmpty(s)) {
                return s;
            }
        }
        return null;
    }

    public String getOrgSubId(String token){
        if (ToolUtil.isNotEmpty(token)) {
            String[] split = token.split(":");
            String s = split[2];
            if (ToolUtil.isNotEmpty(s)) {
                return s;
            }
        }
        return null;
    }


    public void SendMQ(Object obj) {
        //待设计
    }


    public void WriteLog(String log) {
        //待设计
    }

    public void SendMSG(String msg) {
        //待设计
    }
    @Data
    public static class OperateLog{
        private String ActionName;
        private String ActionData;
        private String DataName;
        private String mainDataType;
        private Integer mainDataId;
        private String parameter;
        private String remark;

        public OperateLog(String actionName,String actionData,String DataName){

        }
    }


    public Map<String, Object> isResultMap(List list, SearchCriteriaBase searchCriteriaBase){
        Map<String, Object>  map = new HashMap<>();
        map.put("count",list.size());
        map.put("limit",searchCriteriaBase.getLimit());
        map.put("pageIndex",searchCriteriaBase.getPageIndex());
        map.put("hasMore",false);
        map.put("items",list);
        return  map;
    }

    public boolean isSystem(OperationContext context) {
        try{
            Long id = context.getTenantId();
            if (id == 0L || id == 0) {
                return true;
            }
        }catch (Exception e){
            return  true;
        }
        return false;
    }
}
